const fs = require('fs')

const parser = require('./src/parser')
const downloader = require('./src/downloader')

const torrentPath = process.argv[2] || './torrent/How To Become A Communication Genius - 49 Simple Rules.torrent'
const torrent = parser.readTorrent(torrentPath)

console.log(`Torrent name: ${torrent.info.name}`)
console.log('--------------------')

if (!fs.existsSync('./download')) {
  fs.mkdirSync('./download')
}

let savePath = `./download/${torrent.info.name}`

if (torrent.info.files) {
  if (torrent.info.files.length === 1) {
    savePath = `./download/${torrent.info.files[0].path}`
  } else {
    const saveFolderPath = `./download/${torrent.info.name}`
    if (!fs.existsSync(saveFolderPath)) {
      fs.mkdirSync(saveFolderPath)
    }
  }
}

downloader.start(torrent, savePath)